<template>
  <div>
    <article class="main-breadcrumb">
      <el-breadcrumb separator=">" class="main-breadcrumb-wrap">
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home">维修管理</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home">维修申请</span>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </article>
    <div class="nx-content">
      <div class="flex-bet">
        <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="">
            <el-cascader v-if="userType == 0" v-model="searchForm.companyId" placeholder="请选择公司名称" :options="companyArray"
              :props="{
                checkStrictly: true,
                value: 'id',
                label: 'name',
                children: 'childList',
              }" style="width: 150px" @change="changeTreeItem" clearable></el-cascader>
          </el-form-item>
          <el-form-item label="">
            <el-select v-model="searchForm.baseId" placeholder="请选择基地名称" @change="baseChange"
              style="width: 150px !important" clearable>
              <el-option v-for="item in baseArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <el-select v-model="searchForm.fishpondId" placeholder="请选择鱼池名称" @change="onSubmit"
              style="width: 150px !important" clearable>
              <el-option v-for="item in fishpondArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <el-select v-model="searchForm.status" placeholder="请选择处理状态" @change="onSubmit"
              style="width: 150px !important" clearable>
              <el-option label="待审批" value="0"></el-option>
              <el-option label="维修中" value="1"></el-option>
              <el-option label="待审核" value="2"></el-option>
              <el-option label="已完成" value="3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <el-date-picker v-model="date" type="daterange" align="right" unlink-panels range-separator="至"
              start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" value-format="yyyy-MM-dd"
              @change="handleDate" clearable>
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <div v-allow="'090102'" class="search-btn" @click="onSubmit"></div>
          </el-form-item>
          <el-form-item>
            <div v-allow="'090102'" class="reset-btn" @click="resetForm"></div>
          </el-form-item>
        </el-form>
        <div class="left-title flex" style="margin-top: 0;!important">
          <div v-allow="'090101'" class="add-btn" @click="handleAdd()">
            添加维修申请
          </div>
        </div>
      </div>
      <NxTable :table-cols="tableCols" :table-data="tableData" :pag-ination="pagination" class="table-wrap"
        @refresh="refresh">
        <template slot="faultImages" slot-scope="scope">
          <el-image style="height: 50px" :src="scope.row.faultImages != null
            ? scope.row.faultImages.split(',')[0]
            : ''
            " :preview-src-list="scope.row.faultImages != null
    ? scope.row.faultImages.split(',')
    : []
    " fit="contain">
          </el-image>
        </template>
        <template slot="status" slot-scope="scope">
          <span v-if="scope.row.status == 0">待审批</span>
          <span v-if="scope.row.status == 1">维修中</span>
          <span v-if="scope.row.status == 2">待审核</span>
          <span v-if="scope.row.status == 3">已完成</span>
        </template>
        <template slot="operation" slot-scope="scope">
          <span v-allow="'090108'" @click="handleDetail(scope.row)" class="nx-btn nx-edit">详情</span>
          <span v-allow="'090105'" v-if="scope.row.status == 1" @click="handleRecord(scope.row)"
            class="nx-btn nx-edit">维修记录</span>
          <span v-allow="'090106'" v-if="scope.row.status == 2" @click="handleApproval(scope.row, 2)"
            class="nx-btn nx-edit">审核</span>
          <span v-allow="'090107'" v-if="scope.row.status == 0" @click="handleApproval(scope.row, 1)"
            class="nx-btn nx-edit">审批</span>
          <span v-allow="'090103'" v-if="scope.row.status == 0" @click="handleEdit(scope.row)"
            class="nx-btn nx-edit">编辑</span>
          <span v-allow="'090104'" @click="handleDelete(scope.row)" class="nx-btn nx-del">删除</span>
        </template>
      </NxTable>
    </div>
    <AddEditDialog :addEditId="addEditId" :addEditStatus="addEditStatus" :addEditIsDialog="addEditIsDialog">
    </AddEditDialog>
    <RepairRecord :recordId="recordId" :recordStatus="recordStatus" :recordIsDialog="recordIsDialog"></RepairRecord>
    <RepairDetail :detailId="detailId" :detailStatus="detailStatus" :detailIsDialog="detailIsDialog"></RepairDetail>
  </div>
</template>
<script>
import NxTable from '@/components/NxTable.vue'
import AddEditDialog from './components/addEditDialog.vue'
import RepairRecord from './components/repairRecord.vue'
import RepairDetail from './components/repairDetail.vue'
import { Page } from '@/utils/pagination'
import {
  getDataRepairList,
  delDataRepair,
  approvalDataRepair,
} from '@/api/repair'
import { getObjFishponds } from '@/api/fishpond'
// import { getCompanyInfoTree } from '@/api/company'
// import { getSysCompanyBases } from '@/api/system'
import mixin from '@/api/CompanyInfoTree.js'
export default {
  mixins: [mixin],
  components: {
    NxTable,
    AddEditDialog,
    RepairRecord,
    RepairDetail,
  },
  // 表格数据
  data() {
    return {
      // 查询字段
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        companyId: '',
        baseId: '',
        fishpondId: '',
        status: '',
        startDate: '',
        endDate: '',
      },
      // 表格字段
      tableCols: [
        {
          label: '公司名称',
          prop: 'companyName',
          align: 'center',
          width: 250,
        },
        {
          label: '基地名称',
          prop: 'baseName',
          align: 'center',
          width: 150,
        },
        {
          label: '鱼池名称',
          prop: 'fishpondName',
          align: 'center',
          width: 150,
        },
        {
          label: '故障内容',
          prop: 'content',
          align: 'center',
          width: 150,
        },
        {
          label: '故障图片',
          slot: 'faultImages',
          align: 'center',
          width: 150,
        },
        {
          label: '处理状态',
          slot: 'status',
          align: 'center',
          width: 150,
        },
        {
          label: '申请人',
          prop: 'applyUserName',
          align: 'center',
        },
        {
          label: '申请时间',
          prop: 'applyDate',
          align: 'center',
        },
        {
          label: '操作',
          slot: 'operation',
          align: 'center',
          width: 200,
          fixed: 'right',
        },
      ],
      tableData: [],
      pagination: new Page(),
      date: [], // 日期范围
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            },
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            },
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            },
          },
        ],
      },
      crumbsStatus: '', // 三级面包屑名称
      typeDialog: false,
      addEditIsDialog: false, // 添加编辑页面状态
      recordIsDialog: false, // 维修记录页面状态
      detailIsDialog: false, // 维修详情页面状态
      addEditStatus: 'add',
      recordStatus: 'add',
      detailStatus: 'add',
      addEditId: '', // 添加编辑rowid
      recordId: '', // 维修记录rowid
      detailId: '', // 维修详情rowid
      companyArray: [], // 公司集合
      baseArray: [], // 基地集合
      fishpondArray: [], // 鱼池集合
      userType: '', // 用户类型（0超级管理员，1公司管理员，2公司员工）
    }
  },
  // 监听当baseId发生变化时候自动调用命令
  watch: {
    'searchForm.baseId': {
      handler(newValue, oldValue) {
        this.getObjFishponds()
      },
    }
  },
  // 初始化加载
  mounted() {
    this.userType = Number(window.localStorage.getItem('userType'))
    this.searchForm.userType = this.userType
    this.getCompanyInfoTree()
    this.getList()
  },
  // 自定义方法
  methods: {
    // 查询
    onSubmit() {
      this.pagination.pageNum = 1
      this.getList()
    },
    // 分页查询
    refresh(val) {
      this.searchForm.pageNum = val.pageNum
      this.searchForm.pageSize = val.pageSize
      this.getList()
    },
    // 重置
    resetForm() {
      this.searchForm = {
        pageNum: 1,
        pageSize: 10,
        companyId: '',
        baseId: '',
        fishpondId: '',
        status: '',
        startDate: '',
        endDate: '',
      }

      this.date = []
      this.onSubmit()
    },
    // 查询列表
    getList() {
      getDataRepairList(this.searchForm).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data.data
          this.pagination.count = res.data.total
        }
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          delDataRepair({ id: row.id }).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg,
              })
              this.getList()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    // 审批/审核
    handleApproval(row, status) {
      this.$confirm(
        '正在对该维修申请进行' +
        (status == 1 ? '审批' : '审核') +
        ',' +
        '是否通过?',
        '提示',
        {
          confirmButtonText: '通过',
          cancelButtonText: '取消',
          type: 'warning',
        }
      )
        .then(() => {
          approvalDataRepair({ id: row.id, status: status }).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg,
              })
              this.getList()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消',
          })
        })
    },
    // 添加
    handleAdd() {
      this.addEditId = ''
      this.addEditStatus = 'add'
      this.addEditIsDialog = true
    },
    // 编辑
    handleEdit(row) {
      this.addEditId = row.id + ''
      this.addEditStatus = 'edit'
      this.addEditIsDialog = true
    },
    // 维修记录
    handleRecord(row) {
      this.recordId = row.id + ''
      this.recordStatus = 'add'
      this.recordIsDialog = true
    },
    handleDetail(row) {
      this.detailId = row.id + ''
      this.detailStatus = 'see'
      this.detailIsDialog = true
    },
    // 编辑添加取消关闭
    closeAddEdit() {
      this.addEditIsDialog = false
      this.recordIsDialog = false
      this.detailIsDialog = false
    },
    // 面包屑
    crumbsClick() {
      this.typeDialog = false
    },

    // 用户类型切换
    typeChange() {
      this.searchForm.companyId = ''
      this.companyArray = []

      this.searchForm.roleId = ''
      this.baseArray = []

      // 获取公司列表
      this.getCompanyInfoTree()

      // 查询
      this.onSubmit()
    },
    // 获取公司列表
    // getCompanyInfoTree() {
    //   if (this.searchForm.userType == 0) {
    //     // 超级管理员，公司只有一个总公司
    //     getCompanyInfoTree().then((res) => {
    //       if (res.code == 200) {
    //         this.companyArray = res.data
    //       }
    //     })
    //   } else {
    //     getCompanyInfoTree({ id: 0 }).then((res) => {
    //       if (res.code == 200) {
    //         this.companyArray = res.data
    //       }
    //     })
    //   }
    // },
    // 公司下拉切换
    changeTreeItem(val) {
      if (val) {
        this.searchForm.companyId = val[val.length - 1]
      } else {
        this.searchForm.companyId = ''
      }
      this.searchForm.baseId = ''
      this.baseArray = []
      this.searchForm.fishpondId = ''
      this.fishpondArray = []
      this.getList()
      // 获取基地列表
      this.getSysCompanyBases()
    },
    // 获取基地列表
    // getSysCompanyBases() {
    //   if (
    //     this.searchForm.companyId === '' ||
    //     this.searchForm.companyId == null
    //   ) {
    //     return
    //   }

    //   getSysCompanyBases({ id: this.searchForm.companyId }).then((res) => {
    //     if (res.code == 200) {
    //       this.baseArray = res.data
    //     }
    //   })
    // },
    // 基地下拉切换
    baseChange() {
      this.searchForm.fishpondId = ''
      this.fishpondArray = []

      // 查询
      this.getList()

      // 获取鱼池列表
      this.getObjFishponds()
    },
    // 获取鱼池列表
    getObjFishponds() {
      if (this.searchForm.baseId === '' || this.searchForm.baseId == null) {
        return
      }

      getObjFishponds(this.searchForm).then((res) => {
        if (res.code == 200) {
          this.fishpondArray = res.data
        }
      })
    },
    // 日期切换
    handleDate() {
      if (this.date && this.date.length == 2) {
        this.searchForm.startDate = this.date[0]
        this.searchForm.endDate = this.date[1]
      } else {
        this.searchForm.startDate = ''
        this.searchForm.endDate = ''
      }

      this.getList()
    },
  },
}
</script>
<style lang="less" scoped>
.main-breadcrumb {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 76px;
  border-bottom: solid 20px #f2f2f2;
  padding-left: 20px;

  .main-breadcrumb-wrap {
    font-size: 16px;
    line-height: 68px;

    .main-breadcrumb-home {
      cursor: pointer;
      color: #666666;
      font-weight: bold;
    }

    .main-breadcrumb-sp {
      color: #666666;
      font-weight: bold;
    }
  }
}

/deep/.add-btn {
  margin-top: -1rem !important;
}
</style>
